<?php

namespace App\Transformers\Order;

use App\Models\Order;
use App\Transformers\Cart\CartTransformer;
use App\Transformers\Product\ProductTransformer;
use App\Transformers\User\UserTransformer;
use League\Fractal\TransformerAbstract;

class OrderTransformer extends TransformerAbstract
{
    protected $availableIncludes = ['user','cart','orderLog','orderEconomize','product','orderCartInfo'];

    public function transform(Order $data)
    {
        return [
            'id' => $data->id,
            'user_id' => $data->user_id ?? '',
            'cart_id' => $data->cart_id ?? '',
            'seckill_id' => $data->seckill_id ?? '',
            'bargain_id' => $data->bargain_id ?? '',
            'promoter_id' => $data->promoter_id ?? '',
            'delivery_uid' => $data->delivery_uid ?? '',
            'coupon_id' => $data->coupon_id ?? '',
            'pink_id' => $data->pink_id ?? '',
            'order_number' => $data->order_number ?? '',
            'type' => $data->type ?? '',
            'user_name' => $data->user_name ?? '',
            'user_phone' => $data->user_phone ?? '',
            'user_address' => $data->user_address ?? '',
            'freight_price' => $data->freight_price ?? '',
            'total_num' => $data->total_num ?? '',
            'total_price' => $data->total_price ?? '',
            'total_postage' => $data->total_postage ?? '',
            'pay_price' => $data->pay_price ?? '',
            'pay_postage' => $data->pay_postage ?? '',
            'deduction_price' => $data->deduction_price ?? '',
            'coupon_price' => $data->coupon_price ?? '',
            'pay_time' => $data->pay_time ?? '',
            'pay_type' => $data->pay_type ?? '',
            'status' => $data->status ?? '',
            'status_text' => $data->orderStatusTypeEnum[$data->status] ?? '',
            'refund_status' => $data->refund_status ?? '',
            'refund_status_text' => $data->refundStatusTypeEnum[$data->refund_status] ?? '',
            'refund_reason_wap_img' => $data->refund_reason_wap_img ?? '',
            'refund_reason_wap_explain' => $data->refund_reason_wap_explain ?? '',
            'refund_reason_time' => $data->refund_reason_time ?? '',
            'refund_reason_wap' => $data->refund_reason_wap ?? '',
            'refund_reason' => $data->refund_reason ?? '',
            'refund_price' => $data->refund_price ?? '',
            'delivery_name' => $data->delivery_name ?? '',
            'delivery_code' => $data->delivery_code ?? '',
            'delivery_type' => $data->delivery_type ?? '',
            'delivery_id' => $data->delivery_id ?? '',
            'fictitious_content' => $data->fictitious_content ?? '',
            'gain_integral' => $data->gain_integral ?? '',
            'use_integral' => $data->use_integral ?? '',
            'back_integral' => $data->back_integral ?? '',
            'mark' => $data->mark ?? '',
            'unique' => $data->unique ?? '',
            'remark' => $data->remark ?? '',
            'cost' => $data->cost ?? '',
            'verify_code' => $data->verify_code ?? '',
            'channel_type' => $data->channel_type ?? '',
            'province' => $data->province ?? '',
            'express_dump' => $data->express_dump ?? '',
            'paid' => $data->paid ?? 0,
            'orderLog' => $data->orderLog ?? '',
            'cart' => $data->cart ?? '',
            'deleted_at' => $data->deleted_at ? $data->deleted_at->format("Y-m-d H:i:s") : '',
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
        ];
    }

    /**
     * 订单用户信息
     */
    public function includeUser(Order $order)
    {
        return $this->item($order->user,new UserTransformer());
    }

    /**
     * 额外的商品数据
     */
    public function includeCart(Order $order)
    {
        return $this->collection($order->cart, new CartTransformer());
    }

    /**
     * 订单的操作日志
     */
    public function includeOrderLog(Order $order)
    {
        return $this->collection($order->orderLog, new OrderLogTransformer());
    }

    /**
     * 订单的购物车信息
     */
    public function includeOrderCartInfo(Order $order)
    {
        return $this->item($order->orderCartInfo, new OrderCartInfoTransformer());
    }

    /**
     * 订单使用优惠
     */
    public function includeOrderEconomize(Order $order)
    {
        return $this->item($order->orderEconomize, new OrderEconomizesTransformer());
    }

}
